<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="1200"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="logo" field="institution_logo">
        <sa-upload-image v-model="formData.institution_logo" :limit="3" :multiple="false" />
      </a-form-item>
      <a-form-item label="联系人" field="contact_person">
        <a-input v-model="formData.contact_person" placeholder="请输入联系人" />
      </a-form-item>
      <a-form-item label="联系电话" field="contact_number">
        <a-input v-model="formData.contact_number" placeholder="请输入联系电话" />
      </a-form-item>
      <a-form-item label="是否置顶" field="is_display">
        <sa-switch v-model="formData.is_display" />
      </a-form-item>
      <a-form-item label="金融机构名称" field="institution_name">
        <a-input v-model="formData.institution_name" placeholder="请输入金融机构名称" />
      </a-form-item>
      <a-form-item label="金融分类" field="finance_category">
        <a-select
            v-model="formData.finance_category"
            :options="postData"

            allow-clear
            placeholder="请选择金融分类" />
      </a-form-item>
      <a-form-item label="机构介绍" field="institution_intro">
        <ma-wangEditor v-model="formData.institution_intro" :height="400" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/institution'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const postData = ref([])

let title = computed(() => {
  return '机构列表' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  institution_id: null,
  lang_code: '',
  institution_logo: '',
  contact_person: '',
  contact_number: '',
  is_display: null,
  institution_name: '',
  institution_intro: '',
  finance_category:'',
})

// 验证规则
const rules = {
  lang_code: [{ required: true, message: '语言代码，如zh - CN、en - US等必需填写' }],
  contact_number: [{ required: true, message: '联系电话必需填写' }],
  institution_name: [{ required: true, message: '金融机构名称必需填写' }],
  finance_category: [{ required: true, message: '金融分类必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {
  const postResp = await api.getcate({classification_type:5})
  console.log(postResp.data)
  postData.value = postResp.data
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.institution_id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.institution_id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
